<?php
namespace Crm\Domain;

use Crm\Common\Pager as Pager;
use Crm\Common\MetaData as MetaData;

class WxCustomer
{
    /**
     * pdo连接。
     *
     * @var object
     * @access pdo
     */
    public $pdo;

    public $metaData;
    public $check_option;
    public $unit_option; //商品单位

    public function __construct()
    {
        $this->pdo = \PhalApi\DI()->pdo;
        $this->metaData = new MetaData();

        $this->check_option = \PhalApi\DI()->config->get('basecode.check_option');
        $this->province_code = \PhalApi\DI()->config->get('app.provinceCode'); //省
        $this->city_code = \PhalApi\DI()->config->get('app.cityCode'); //城市
        $this->unit_option = $this->metaData->getPairBaseCodeByType(20202); //商品单位
    }

    /* 新增客户 */
    public function add($user_id,$data){
        $data['create_date'] = time();
        $data['create_uid'] = $user_id;
        $data['intent_date'] = $data['intent_date'] ? strtotime(date('Y-m-d 00:00:00',strtotime($data['intent_date']))) : strtotime(date('Y-m-d 00:00:00',time()));
        $data['flag'] = 1;
        //判断同一用户同一天是否提交预约
        $start_date = strtotime(date('Y-m-d 00:00:00',time()));
        $end_date   = strtotime(date('Y-m-d 23:59:59',time()));
        $sql = "select count(*) as cnt from wx_customer 
                where flag=1 and 
                      project_id=".$data['project_id']." and 
                      mall_id=".$data['mall_id']." and 
                      origin=".$data['origin']." and 
                      origin_network=".$data['origin_network']." and mobile='".$data['mobile']."'";
        $intent = $this->pdo->getRow($sql);
        //status: 1预约 2报备 3意向 4到访 0无效
        if($intent['cnt']>0){
            if($data['status']==1) {
                $result = array('status' => '200', 'msg' => '您已预约成功!');
            }else if($data['status']==2){
                $result = array('status' => '200', 'msg' => '您已报备该客户!');
            }else{
                $result = array('status' => '200', 'msg' => '您已添加该客户!');
            }
        }else{
            $rlt = $this->pdo->add($data, 'wx_customer');
            if($rlt) {
                if($data['status']==1) {
                    $result = array('status' => '200', 'msg' => '恭喜您,预约成功!');
                }else if($data['status']==2){
                    $result = array('status' => '200', 'msg' => '恭喜您,报备成功!');
                }else if($data['status']==3){
                    $result = array('status' => '200', 'msg' => '恭喜您,添加意向成功!');
                }else if($data['status']==4){
                    $result = array('status' => '200', 'msg' => '恭喜您,添加到访成功!');
                }else{
                    $result = array('status' => '200', 'msg' => '恭喜您,操作成功!');
                }
            }else{
                $result = array('status' => '201', 'msg' => '很抱歉,操作失败!');
            }
        }
        return $result;
    }


}
